﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AutoFix.controls;
using BusinessLogicLayer.Manager;
using Objects;
using Common;
using AutoFix.Base;
namespace AutoFix
{
    public partial class model_list : BasePage
    {
        #region [Private Members]
		/// <summary>
		/// Holds the articles for comparison
		/// </summary>
        private List<Article> articles = null; 
	    #endregion

        #region [BasePage Implementations]
        /// <summary>
        /// Gets or sets a value indicating whether [is login page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is login page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsLoginPage { get { return false; } }

        /// <summary>
        /// Gets or sets a value indicating whether [is anonymous page].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [is anonymous page]; otherwise, <c>false</c>.
        /// </value>
        protected override bool IsAnonymousPage { get { return false; } }

        /// <summary>
        /// Page name
        /// </summary>
        protected override string PageName
        {
            get { return "Model List"; }
        }

        /// <summary>
        /// Gets the authorized user types.
        /// </summary>
        protected override List<UserType> AuthorizedUserTypes
        {
            get { return new List<UserType>(new UserType[] { UserType.Admin, UserType.Contributor }); }
        }

        #endregion

        #region [Event Handlers]
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadArticles();
                LoadPage();
            }
        }
        /// <summary>
        /// Handles the item command event of the Datalist control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void dtlModelList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":

                    VehicleModelTypeManager vehicleModelTypeManager = new VehicleModelTypeManager();
                    VehicleModelType vehicleModelType = new VehicleModelType();
                    HiddenField hdnVehicleModelTypeName = (HiddenField)e.Item.FindControl("hdnModelTypeName");
                    HiddenField hdnModelID = (HiddenField)e.Item.FindControl("hdnModelID");
                    try
                    {

                        vehicleModelType.VehicleModelTypeID = Convert.ToInt32(e.CommandArgument);
                        vehicleModelType.VehicleModelTypeName = hdnVehicleModelTypeName.Value;
                        vehicleModelType.VehicleModelID = Convert.ToInt32(hdnModelID.Value);
                        vehicleModelType.IsActive = false;

                        bool success = vehicleModelTypeManager.UpdateVehicleModelType(vehicleModelType);
                        if (success == true)
                        {
                            LoadPage();
                        }
                        else
                        {
                            this.pageActionMessage.InnerText = "Unable to delete vehicle model from the system";
                            this.pageActionMessage.Attributes.Add("class", "error-msg");
                        }
                    }
                    catch (Exception)
                    {
                        this.pageActionMessage.InnerText = "Unable to delete vehicle model from the system";
                        this.pageActionMessage.Attributes.Add("class", "error-msg");
                    }
                    finally
                    {
                        vehicleModelType = null;
                        vehicleModelTypeManager = null;
                    }

                    break;
                default:
                    break;
            } 
        }
        /// <summary>
        /// Handles the item data bound event of the Datalist control.
        /// </summary>
        /// <param name="source">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void dtlModelList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            VehicleModelsVehicleModelID vehicleModelDetails = (VehicleModelsVehicleModelID)e.Item.DataItem;
            bool containsVehicleinfo = articles.Exists(a=>a.VehicleModelID == vehicleModelDetails.VehicleModelID && a.VehicleModelTypeID == vehicleModelDetails.VehicleModelTypeID && a.VehicleModelTypeYearID == vehicleModelDetails.VehicleModelTypeYearID && a.IsPublished);
            Button btnDeleteModel = (Button)e.Item.FindControl("btnDeleteModel");
            if (containsVehicleinfo)
            {
                btnDeleteModel.Visible = false;
            }
        }
        #endregion

        #region [Private Methods]

        /// <summary>
        /// Loads all the articles to check if vehicle model info is used
        /// </summary>
        private void LoadArticles()
        {
            ArticleManager articleManager = new ArticleManager();
            List<Article> articles = null;
            try
            {
                articles = articleManager.GetArticles();
                if (articles != null && articles.Count > 0)
                {
                    this.articles = articles;
                }
            }
            catch
            {
            }
            finally
            {
                articleManager = null;
                articles = null;
            }
        }
        /// <summary>
        /// Binds the model list
        /// </summary>
        private void LoadPage()
        {

            VehicleModelManager vehicleModelManager = new VehicleModelManager();
            List<VehicleModelsVehicleModelID> vehicleModelsVehicleModelID = null;
            List<VehicleModelsVehicleModelID> modelDataSource = null;

            try
            {
                vehicleModelsVehicleModelID = vehicleModelManager.VehicleModelDetailsByVehicleModelID();
                foreach (VehicleModelsVehicleModelID item in vehicleModelsVehicleModelID)
                {
                    if (!(item.VehicleModelTypeImageURL.Contains("imgNoModelImage.png")))
                    {
                        string fullImagePath = AppConfiguration.VehicleImagePath + item.VehicleModelTypeImageURL.Substring(0, item.VehicleModelTypeImageURL.IndexOf(".jpg")) + "/" + item.VehicleModelTypeImageURL;
                        item.VehicleModelTypeImageURL = fullImagePath;

                    }

                }
                modelDataSource = vehicleModelsVehicleModelID.Where(vmvmid => vmvmid.IsActive == true).ToList();
                if (modelDataSource.Count > 0)
                {
                    this.dtlModelList.DataSource = modelDataSource;
                    this.dtlModelList.DataBind();
                }
                else
                {
                    this.pageActionMessage.InnerText = "There are no active models in the system.";
                    this.pageActionMessage.Attributes.Add("class", "information-msg");
                }
            }
            catch (Exception)
            {

                this.pageActionMessage.InnerText = "Failed to load the vehicle models.";
                this.pageActionMessage.Attributes.Add("class", "error-msg");
            }
            finally
            {
                vehicleModelsVehicleModelID = null;
                vehicleModelManager = null;
                modelDataSource = null;
            }
        }
        #endregion
 

    }
}